package com.blackhorse.work;

import com.blackhorse.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//工作模式生产者，只有一个
public class Producer {

    public static final String QUEUE_NAME = "work_queue";

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            connection = ConnectionUtils.getConnection();
            channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);//创建队列
            for (int i = 0; i < 30; i++) {
                String msg = "hello，小兔子我来了！" + "work模式，第" + i + "条";
                channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());//发布消息
                System.out.println("已发送消息：" + msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.close(channel, connection);
        }

    }
}
